'use strict';

const Service = require('egg').Service;
// const md5 = require('js-md5')
const {
    ERROR,
    SUCCESS,
} = require('../util/util');

class UserService extends Service {
    async create({ username, password }) {
        const {
            ctx,
        } = this;
        try {
            if (username && password) {
                // 判断用户是否存在
                const res = await ctx.model.User.findOne({ where: { username } })
                    // 存在
                if (res) {
                    return {
                        ...ERROR,
                        msg: '用户名已存在!'
                    }
                }
                try {
                    const res = await ctx.model.User.create({ username, password })
                    return {
                        ...SUCCESS,
                        data: res.dataValues,
                        password: '***',
                        msg: "添加用户成功！"
                    }
                } catch (e) {
                    return {
                        ...ERROR,
                        msg: e.errors[0].message,
                    }
                }

            } else if (username) {
                return {
                    ...ERROR,
                    msg: '请输入密码！'
                }
            }
            return {
                ...ERROR,
                msg: "请输入用户名！"
            }

        } catch (error) {
            ctx.status = 500;
            throw (error);
        }
    }

    // 登录
    async login({ username, password }) {
        const {
            ctx,
        } = this;
        try {
            const user = await ctx.model.User.findOne({
                where: {
                    username: username,
                },
            });

            if (!user) {
                return {
                    ...ERROR,
                    msg: '用户不存在',
                }
            }

            if (password === user.password) {
                ctx.status = 200;
                return {
                    ...SUCCESS,
                    data: Object.assign(user, { password: '***' }),
                    msg: '登录成功',
                    token: await ctx.service.actionToken.create(user.username)
                }

            }
            return {
                ...ERROR,
                msg: '密码错误'
            }

        } catch (error) {
            ctx.status = 500;
            throw (error);
        }
    }

    // 获取用户信息
    async userInfo() {
        const {
            ctx,
        } = this;

        const token = ctx.headers.authorization ? ctx.headers.authorization : '';
        const username = ctx.app.jwt.verify(token.substring(7), ctx.app.config.jwt.secret).data.username //解析token
        try {
            const user = await ctx.model.User.findOne({
                where: {
                    username
                },
            });
            ctx.status = 200;

            if (!user) {
                return {
                    ...SUCCESS,
                    msg: "没有该用户！"
                }
            }
            return {
                ...SUCCESS,
                data: {...user.dataValues, password: '****' },
                msg: '获取用户信息成功',
            }
        } catch (error) {
            ctx.status = 500;
            throw (error);
        }
    }


}

module.exports = UserService;